/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controls;

import com.aptech.bluepumpkin.Event;
import com.aptech.bluepumpkin.Prize;
import com.aptech.bluepumpkin.Registration;
import com.aptech.bluepumpkin.Winner;
import com.aptech.bluepumpkin.beans.EventFacade;
import com.aptech.bluepumpkin.beans.PrizeFacade;
import com.aptech.bluepumpkin.beans.RegistrationFacade;
import com.aptech.bluepumpkin.beans.WinnerFacade;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import tools.PrizeDataModel;
import tools.RegistrationDataModel;

/**
 *
 * @author Trong Nguyen
 */
@ManagedBean
@SessionScoped
public class WinnerControl implements Serializable {

    @EJB
    WinnerFacade winnerFacade;
    @EJB
    PrizeFacade prizeFacade;
    @EJB
    RegistrationFacade registrationFacade;
    @EJB
    EventFacade eventFacade;
    private Winner winner;
    private Winner selectedWinner;
    private Event event = new Event();
    private Registration registration;
    private Registration oldRegistration;
    private Prize prize;
    private Prize oldPrize;
    private PrizeDataModel prizeDataModel;
    private RegistrationDataModel registrationDataModel;
    private boolean isUpdating = false;

    public PrizeDataModel prizeModelByEvent() {
        if (null == event) {
            return null;
        }
        if (isUpdating) {
            if (null != prize && null != event.getPrizeList()) {
                event.getPrizeList().add(prize);
            } else {
                System.out.println("--IsUpdating = " + isUpdating + ", prize is NULL");
            }

        }
        prizeDataModel = new PrizeDataModel(event.getPrizeList());
        return prizeDataModel;
    }

    public RegistrationDataModel registrationDataModelByEvent() {
        if (null == event) {
            return null;
        }
        if (isUpdating) {
            if (null != registration && null != event.getRegistrationList()) {
                event.getRegistrationList().add(registration);
            } else {
                System.out.println("--IsUpdating = " + isUpdating + ", registration is NULL");
            }

        }
        registrationDataModel = new RegistrationDataModel(event.getRegistrationList());
        return registrationDataModel;
    }

    public void reloadTables() {
        List<Event> buildAvailableListEvent = buildAvailableListEvent();
        int indexOf = buildAvailableListEvent.indexOf(event);
        if (indexOf >= 0) {
            event = buildAvailableListEvent.get(indexOf);
        } else {
            event = new Event();
        }
    }

    public List<Event> comlpleteMethod(String query) {
        List<Event> allActiveEvents = buildAvailableListEvent();
        List<Event> filter = new ArrayList<Event>();
        for (Event event1 : allActiveEvents) {
            if (event1.getTitle().contains(query)) {
                filter.add(event1);
            }

        }
        return filter;
    }

    public List<Event> buildAvailableListEvent() {
        List<Winner> allWinners = winnerFacade.findAll();
        List<Prize> removePrizes = new ArrayList<Prize>();
        for (Winner w : allWinners) {
            removePrizes.add(w.getPrize());
        }
        List<Prize> allPrizes = prizeFacade.getAllPrizes();
        // list Available prizes
        allPrizes.removeAll(removePrizes);
        List<Event> availableEvents = new ArrayList<Event>();
        // get available events
        for (Prize p : allPrizes) {
            if (!availableEvents.contains(p.getEvent())) {
                availableEvents.add(p.getEvent());
            }
        }
        return availableEvents;

    }

    public List<Winner> getAllWinners() {
        return winnerFacade.getAllWinners();
    }

    public void insert() {
        if (null == registration) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "You MUST choose an User ", ""));
            return;
        }
        if (null == prize) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "You MUST choose a Prize", ""));
            return;
        }
        winner = new Winner();
        winner.setPrize(getPrize());
        winner.setRegistration(getRegistration());
        winnerFacade.create(winner);
        prize.getWinnerList().add(winner);
        prizeFacade.edit(prize);
        registration.getWinnerList().add(winner);
        registrationFacade.edit(registration);
        prize = null;
        registration = null;
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Winner is added.", ""));
        reloadTables();
    }

    public void updateWinner(Winner w) {
        isUpdating = true;
        System.out.println("------------updateWinner------------------");
        selectedWinner = w;
        event = selectedWinner.getPrize().getEvent();
        oldPrize = selectedWinner.getPrize();
        prize = selectedWinner.getPrize();
        registration = selectedWinner.getRegistration();
        oldRegistration = selectedWinner.getRegistration();
        event.getPrizeList().add(prize);
        event.getRegistrationList().add(registration);
        redirectToEditWinner();
    }

    public void update() {
        if (null == registration) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "You MUST choose an User ", ""));
            return;
        }
        if (null == prize) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "You MUST choose a Prize", ""));
            return;
        }
        // modify prize
        if (!oldPrize.equals(prize)) {
            // remove winner
            oldPrize.getWinnerList().remove(selectedWinner);
            prizeFacade.edit(oldPrize);
            // add winner
            prize.getWinnerList().add(selectedWinner);
            prizeFacade.edit(prize);
        }
        if (!oldRegistration.equals(registration)) {
            // remove winner
            oldRegistration.getWinnerList().remove(selectedWinner);
            registrationFacade.edit(oldRegistration);
            // add winner
            registration.getWinnerList().add(selectedWinner);
            registrationFacade.edit(registration);
        }
        winnerFacade.edit(selectedWinner);
        prize = null;
        registration = null;
        isUpdating = false;
        redirectToListWinner();

    }

    public void delete(Winner w) {
        selectedWinner = w;
        Prize prize1 = selectedWinner.getPrize();
        prize1.getWinnerList().remove(selectedWinner);
        prizeFacade.edit(prize1);
        Registration registration1 = selectedWinner.getRegistration();
        registration1.getWinnerList().remove(selectedWinner);
        registrationFacade.edit(registration1);
        winnerFacade.remove(selectedWinner);
        prize = null;
        registration = null;
    }

    public void redirectToAddWinner() {
        FacesContext context = FacesContext.getCurrentInstance();
        String path = context.getExternalContext().getRequestContextPath() + "/faces/admin/winner/addWinner.xhtml";
        utils.Uility.redirectPage(context, null, path);
    }

    public void redirectToEditWinner() {
        FacesContext context = FacesContext.getCurrentInstance();
        String path = context.getExternalContext().getRequestContextPath() + "/faces/admin/winner/editWinner.xhtml";
        utils.Uility.redirectPage(context, null, path);
    }

    public void redirectToListWinner() {
        FacesContext context = FacesContext.getCurrentInstance();
        String path = context.getExternalContext().getRequestContextPath() + "/faces/admin/winner/winnerPage.xhtml";
        utils.Uility.redirectPage(context, null, path);
    }

    public Winner getSelectedWinner() {
        return selectedWinner;
    }

    public void setSelectedWinner(Winner selectedWinner) {
        this.selectedWinner = selectedWinner;
    }

    public Winner getWinner() {
        return winner;
    }

    public void setWinner(Winner winner) {
        this.winner = winner;
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public Prize getPrize() {
        return prize;
    }

    public void setPrize(Prize prize) {
        this.prize = prize;
    }

    public Registration getRegistration() {
        return registration;
    }

    public void setRegistration(Registration registration) {
        this.registration = registration;
    }

    public WinnerControl() {
    }
}
